package com.ymatou.query.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.trading.api.parameter.GetExportOrderInfoBean;
import com.ymatou.query.trading.api.service.GetExportOrderInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.entitie.ExportOrderInfo;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取导出订单信息 FreeMaker Template自动生成代码 接口未使用
 */
@TestSuite
public class Ts_GetExportOrderInfo {

	private static GetExportOrderInfoBean geoibean;
	private static GetExportOrderInfoCall geoicall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取导出订单信息");
	}

	@Before
	public void caseUp() {

		geoibean = new GetExportOrderInfoBean();
		geoicall = new GetExportOrderInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(geoicall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetExportOrderInfo_001() {
		Logger.start(true, "获取导出订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 1;
			geoibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-1);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(1);
			// 开始时间
			geoibean.setStartDate(StartDate);
			// 结束时间
			geoibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(4);
			geoibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(0);
			geoibean.setOrderTypes(OrderTypesList);
			geoibean.setSearchText(null);
			// 卖家ID
			geoibean.setUserId(SellerId);
			// 发送请求
			geoicall.setData(geoibean);
			geoicall.callService();
			Logger.comment("geoicall send:" + geoicall.getEntityString());
			Logger.comment("geoicall response:" + geoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, geoicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, geoicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true,
					geoicall.getExportOrderInfoList().size() > 0, "验证结果长度");
			// Logger.debug("结果长度:"+geoicall.getExportOrderInfoList().size());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetExportOrderInfo_002() {
		Logger.start(true, "获取导出系统自动取消订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int oid = getBuyerOrderId();
			new YmtOrdersWapper().updateOrderStatusByOrderId(oid,
					OrderStatusEnum.SystemCancel.getValue());
			int DateType = 1;
			geoibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(1);
			// 开始时间
			geoibean.setStartDate(StartDate);
			// 结束时间
			geoibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(OrderStatusEnum.SystemCancel.getValue());
			geoibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(OrderTypeEnum.Shangou.getValue());
			geoibean.setOrderTypes(OrderTypesList);
			geoibean.setSearchText(null);
			// 卖家ID
			geoibean.setUserId(SellerId);
			// 发送请求
			geoicall.setData(geoibean);
			geoicall.callService();
			Logger.comment("geoicall send:" + geoicall.getEntityString());
			Logger.comment("geoicall response:" + geoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, geoicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, geoicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true,
					geoicall.getExportOrderInfoList().size() > 0, "验证结果长度");
			// Logger.debug("结果长度:"+geoicall.getExportOrderInfoList().size());
			ArrayList<ExportOrderInfo> exportOrderInfoList = geoicall
					.getExportOrderInfoList();
			for (ExportOrderInfo exportOrderInfo : exportOrderInfoList) {
				Logger.verifyEquals(OrderStatusEnum.SystemCancel.getValue(),
						exportOrderInfo.getOrderState(), "验证OrderState");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetExportOrderInfo_003() {
		Logger.start(true, "获取导出订单状态为2-已付款的订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 1;
			geoibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(1);
			// 开始时间
			geoibean.setStartDate(StartDate);
			// 结束时间
			geoibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(OrderStatusEnum.AccountPaid.getValue());
			// OrderStatesList.add(OrderStatusEnum.OrderPostPay.getValue());

			geoibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(OrderTypeEnum.Shangou.getValue());
			geoibean.setOrderTypes(OrderTypesList);
			geoibean.setSearchText(null);
			// 卖家ID
			geoibean.setUserId(SellerId);
			// 发送请求
			geoicall.setData(geoibean);
			geoicall.callService();
			Logger.comment("geoicall send:" + geoicall.getEntityString());
			Logger.comment("geoicall response:" + geoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, geoicall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetExportOrderInfo_004() {
		Logger.start(true, "获取导出等待发货订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 2;
			geoibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(1);
			// 开始时间
			geoibean.setStartDate(StartDate);
			// 结束时间
			geoibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			// OrderStatesList.add(OrderStatusEnum.SellerAccept.getValue());
			OrderStatesList.add(OrderStatusEnum.OrderPostPay.getValue());
			geoibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(OrderTypeEnum.Shangou.getValue());
			geoibean.setOrderTypes(OrderTypesList);
			geoibean.setSearchText(null);
			// 卖家ID
			geoibean.setUserId(SellerId);
			// 发送请求
			geoicall.setData(geoibean);
			geoicall.callService();
			Logger.comment("geoicall send:" + geoicall.getEntityString());
			Logger.comment("geoicall response:" + geoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, geoicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, geoicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true,
					geoicall.getExportOrderInfoList().size() > 0, "验证结果长度");
			// Logger.debug("结果长度:"+geoicall.getExportOrderInfoList().size());
			ArrayList<ExportOrderInfo> exportOrderInfoList = geoicall
					.getExportOrderInfoList();
			for (ExportOrderInfo exportOrderInfo : exportOrderInfoList) {
				Logger.verifyEquals(OrderStatusEnum.SellerAccept.getValue(),
						exportOrderInfo.getOrderState(), "验证OrderState");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetExportOrderInfo_005() {
		Logger.start(true, "获取导出补款完成订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 2;
			geoibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(1);
			// 开始时间
			geoibean.setStartDate(StartDate);
			// 结束时间
			geoibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(OrderStatusEnum.SellerAccept.getValue());
			geoibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			geoibean.setOrderTypes(OrderTypesList);
			geoibean.setSearchText(null);
			// 卖家ID
			geoibean.setUserId(SellerId);
			// 发送请求
			geoicall.setData(geoibean);
			geoicall.callService();
			// Logger.comment("geoicall send:" + geoicall.getEntityString());
			// Logger.comment("geoicall response:" + geoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, geoicall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetExportOrderInfo_006() {
		Logger.start(true, "获取导出17和101状态的订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 2;
			geoibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(1);
			// 开始时间
			geoibean.setStartDate(StartDate);
			// 结束时间
			geoibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(OrderStatusEnum.SellerAccept.getValue());
			OrderStatesList.add(OrderStatusEnum.OrderPostPay.getValue());
			geoibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(OrderTypeEnum.Shangou.getValue());
			geoibean.setOrderTypes(OrderTypesList);
			geoibean.setSearchText(null);
			// 卖家ID
			geoibean.setUserId(SellerId);
			// 发送请求
			geoicall.setData(geoibean);
			geoicall.callService();
			Logger.comment("geoicall send:" + geoicall.getEntityString());
			Logger.comment("geoicall response:" + geoicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, geoicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, geoicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true,
					geoicall.getExportOrderInfoList().size() > 0, "验证结果长度");
			// Logger.debug("结果长度:"+geoicall.getExportOrderInfoList().size());
			ArrayList<ExportOrderInfo> exportOrderInfoList = geoicall
					.getExportOrderInfoList();
			for (ExportOrderInfo exportOrderInfo : exportOrderInfoList) {
				Logger.verifyEquals(OrderStatusEnum.SellerAccept.getValue(),
						exportOrderInfo.getOrderState(), "验证OrderState");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 创建一个支付过的订单
	 * 
	 * @return
	 */
	public static int getBuyerOrderId() {
		int orderId = 0;
		try {
			// 创建订单
			Map map = new OrderCallService().appSaveOrder();
			orderId = (Integer) map.get("orderId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 买家付款
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
		} catch (Exception e) {
			e.printStackTrace();
			return orderId;
		}
		return orderId;
	}
}